package com.iyao_app.adapter;

import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.StoryDetail;
import com.iyao_app.entry.User;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyao_app.util.CountUtils;
import com.iyao_app.util.DbUtil;
import com.iyao_app.util.ResourcesUtils;
import com.iyao_app.util.TimeFormat;
import com.iyaowoqu.base_library.util.TimeUtils;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.rockerhieu.emojicon.EmojiconTextView;

/**
 * 作者 :  胡家兵
 * 时间 :  2017/12/11.
 */

public class StoryListAdapter_ extends BaseQuickAdapter<StoryDetail, StoryListAdapter_.ViewHolder> {


    public StoryListAdapter_(@Nullable List<StoryDetail> data) {
        super(R.layout.item_story_list, data);
    }

    @Override
    protected void convert(final ViewHolder helper, final StoryDetail item) {
        if (item.isLast) {
            helper.mLinTime.setVisibility(View.VISIBLE);
            helper.mTvTime.setText("哎吆, 没有更多了哦~ ");
            helper.mRlRoot.setVisibility(View.GONE);
            helper.mTvWeek.setText("");
        } else {
            helper.mRlRoot.setVisibility(View.VISIBLE);
            if (item.isNotDayStart) {
                helper.mLinTime.setVisibility(View.GONE);
            } else {
                helper.mLinTime.setVisibility(View.VISIBLE);
            }
            helper.mTvTime.setText(TimeFormat.getSignTime(item.createTime));
            helper.mTvWeek.setText(String.format("星期%s", TimeUtils.getChineseWeek(item.createTime)));
            try {

                Uri uri = Uri.parse(item.avatarUrl);
                ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                        .setResizeOptions(new ResizeOptions(R.dimen.x60, R.dimen.x60))
                        .build();
                PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                        .setOldController(helper.mSdvHead.getController())
                        .setImageRequest(request)
                        .build();
                helper.mSdvHead.setController(controller);
            } catch (Exception e) {
                e.printStackTrace();
            }
            helper.mTvName.setText(item.nickname);
            helper.mTvLocation.setText(item.sceneName);
            if (item.goodVote) {
                helper.mIvLike.setImageResource(R.mipmap.btn_xihuan);
            } else {
                helper.mIvLike.setImageResource(R.mipmap.btn_wuxianxihuan);
            }
            String goodCount = item.goodCount;
            if (TextUtils.isEmpty(goodCount)) {
                helper.mTvLike.setText(String.format("赞 (%s)", "0"));
                item.count = 0;
            } else {
                helper.mTvLike.setText(String.format("赞 (%s)", CountUtils.getCount(item.goodCount)));
                item.count = Integer.parseInt(goodCount);
            }


            helper.mTvComment.setText(String.format("评论 (%s)", CountUtils.getCount(item.commentCount)));

            if (TextUtils.isEmpty(item.eventDesc)) {
                helper.mLinV2.setVisibility(View.GONE);
            } else {
                helper.mLinV2.setVisibility(View.VISIBLE);
                helper.mTvContent.setText(item.eventDesc);
            }
            if (item.eventFileUrlList != null && item.eventFileUrlList.size() > 0) {
                helper.mLinV3.setVisibility(View.VISIBLE);
                StaggeredGridLayoutManager layoutManager = null;
                OnlyPictureAdapter adapter = null;
                if (item.eventFileUrlList.size() == 0) {
                    layoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
                    adapter = new OnlyPictureAdapter(item.eventFileUrlList);
                } else {
                    layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
                    adapter = new OnlyPictureAdapter(item.eventFileUrlList);
                }
                adapter.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        if (pictureClickListener != null) {
                            pictureClickListener.click(item.eventFileUrlList, position);
                        }
                    }
                });
                helper.mRecyclerView.setAdapter(adapter);
                helper.mRecyclerView.setLayoutManager(layoutManager);

            } else {
                helper.mLinV3.setVisibility(View.GONE);
            }

        }
        helper.mRlLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                like(item, helper.mTvLike, helper.mIvLike);
            }
        });
    }

    private void like(final StoryDetail item, final TextView tv, final ImageView iv) {
        User user = DbUtil.getUser();
        NetUtils.getInstance().goodOrBad(item.id, user.id, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    item.count += 1;
                    item.goodCount = String.valueOf(item.count);
                    item.goodVote = false;
                    tv.setText(String.format("赞 (%s)", CountUtils.getCount(item.goodCount)));
                    tv.setTextColor(ResourcesUtils.getColor(R.color.app_orange_ff773d));
                    iv.setImageResource(R.mipmap.btn_wuxianxihuan);
                }

            }

            @Override
            public void onFail(String message, int code) {

            }
        }, null);
    }

    OnPictureClickListener pictureClickListener;

    public void setOnPictureClickListener(OnPictureClickListener onPictureClickListener) {
        pictureClickListener = onPictureClickListener;
    }

    public interface OnPictureClickListener {
        void click(List<String> data, int position);
    }

    static class ViewHolder extends BaseViewHolder {
        @BindView(R.id.tv_time)
        TextView mTvTime;
        @BindView(R.id.tv_week)
        TextView mTvWeek;
        @BindView(R.id.lin_time)
        LinearLayout mLinTime;
        @BindView(R.id.v_line)
        View mVLine;
        @BindView(R.id.sdv_head)
        SimpleDraweeView mSdvHead;
        @BindView(R.id.tv_name)
        EmojiconTextView mTvName;
        @BindView(R.id.tv_location)
        TextView mTvLocation;
        @BindView(R.id.iv_like)
        ImageView mIvLike;
        @BindView(R.id.tv_like)
        TextView mTvLike;
        @BindView(R.id.iv_comment)
        ImageView mIvComment;
        @BindView(R.id.tv_comment)
        TextView mTvComment;
        @BindView(R.id.lin_v_1)
        LinearLayout mLinV1;
        @BindView(R.id.tv_content)
        EmojiconTextView mTvContent;
        @BindView(R.id.lin_v_2)
        LinearLayout mLinV2;
        @BindView(R.id.recyclerView)
        RecyclerView mRecyclerView;
        @BindView(R.id.lin_v_3)
        LinearLayout mLinV3;
        @BindView(R.id.rl_root)
        LinearLayout mRlRoot;
        @BindView(R.id.lin_like)
        LinearLayout mRlLike;

        ViewHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
